$(document).ready(function() {
    // 从 URL 获取订单信息
    const urlParams = new URLSearchParams(window.location.search);
    const orderId = urlParams.get('orderId');
    const amount = urlParams.get('amount');
    const paymentMethod = urlParams.get('paymentMethod');
    const codeUrl = urlParams.get('codeUrl');

    // 显示订单信息
    $('#payAmount').text(parseFloat(amount).toFixed(2));
    $('#orderNumber').text(orderId);
    $('#paymentMethod').text(getPaymentMethodName(paymentMethod));
    
    // 生成二维码
    generateQRCode(codeUrl);
    
    // 启动倒计时
    startCountdown();
});

// 获取支付方式名称
function getPaymentMethodName(method) {
    const methods = {
        '1': '支付宝',
        '2': '微信支付',
        '0': '银联支付'
    };
    return methods[method] || '支付宝';
}

// 生成二维码
function generateQRCode(codeUrl) {
    new QRCode(document.getElementById("qrcode"), {
        text: codeUrl,
        width: 200,
        height: 200,
        colorDark: "#000000",
        colorLight: "#ffffff",
        correctLevel: QRCode.CorrectLevel.H
    });
}

// 倒计时功能
function startCountdown() {
    let minutes = 1;
    let seconds = 59;
    
    const timer = setInterval(() => {
        if (minutes === 0 && seconds === 0) {
            clearInterval(timer);
            // 订单超时处理
            handleOrderTimeout();
            return;
        }
        
        if (seconds === 0) {
            minutes--;
            seconds = 59;
        } else {
            seconds--;
        }
        
        $('#timeRemaining').text(
            `${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`
        );
    }, 1000);
}

// 订单超时处理
function handleOrderTimeout() {
    alert('支付超时，请重新下单');
    window.location.href = 'index.html';
} 